package com.dashu.device.tools;

import com.dashu.base.enums.device.PicType;
import com.dashu.base.utils.File.FileUtils;
import com.dashu.base.utils.File.PicUtils;
import com.dashu.base.utils.OssClienUtils;
import com.dashu.base.utils.SystemUtils;
import com.dashu.base.utils.date.TimeUtils;
import com.dashu.device.bean.PictureBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;

import java.io.File;

@Slf4j
public class Path {

    /**
     * 保存图片的路径
     */
    public static String save_pic_path = SystemUtils.isOSWindows() ? "G:\\bug\\pic\\" : "/Image/";


    /**
     * 图片 base64 输出
     *
     * @param picType  输入图片类型
     * @param content  输入图片内容
     * @param iden     身份
     * @param personId 唯一码
     * @return 返回base64图片
     */
    public static String printBase64Pic(PicType picType, String content, String iden, String personId) {
        try {
            String picName = iden + "_" + personId + ".jpg";

            String path = Path.save_pic_path + TimeUtils.getNowYear() + File.separatorChar + TimeUtils.getNowMonth() + File.separatorChar + TimeUtils.getNowDay();
            FileUtils.isPathExist(path);

            if (picType == PicType.URL) {
                //先下载图片
                FileUtils.downPic(content, Path.save_pic_path + TimeUtils.getNowYear() + File.separatorChar + TimeUtils.getNowMonth() + File.separatorChar + TimeUtils.getNowDay(), picName);
                //进行图片是否裁剪  960*960
                PicUtils.resizeImage(path + File.separatorChar + picName, path + File.separatorChar + picName, 960, 960);
                //将图片转base64
                return PicUtils.picTobase64(path + File.separatorChar + picName);
            } else if (picType == PicType.BASE64) {
                //将图片存储在本地
                PicUtils.base64Topic(path + File.separatorChar + picName, content);

                //进行图片是否裁剪  960*960
                PicUtils.resizeImage(path + File.separatorChar + picName, path + File.separatorChar + picName, 960, 960);

                //将图片转base64
                return PicUtils.picTobase64(path + File.separatorChar + picName);
            }
            return null;
        } catch (Exception e){
            log.error("Path.printBase64Pic" , e);
            return null;
        }
    }



    /**
     * 图片 本地连接 输出
     *
     * @param picType  输入图片类型
     * @param content  输入图片内容
     * @param iden     身份
     * @param personId 唯一码
     * @return 返回base64图片
     */
    public static String printBase64Pic2(PicType picType, String content, String iden, String personId) {
        String picName = iden + "_" + personId + ".jpg";

        String path = Path.save_pic_path + TimeUtils.getNowYear() + File.separatorChar + TimeUtils.getNowMonth() + File.separatorChar + TimeUtils.getNowDay();
        FileUtils.isPathExist(path);

        if (picType == PicType.URL) {
            //先下载图片
            FileUtils.downPic(content, Path.save_pic_path + TimeUtils.getNowYear() + File.separatorChar + TimeUtils.getNowMonth() + File.separatorChar + TimeUtils.getNowDay(), picName);
            //进行图片是否裁剪  960*960
            PicUtils.resizeImage(path + File.separatorChar + picName, path + File.separatorChar + picName, 960, 960);
            //将图片转base64
            return path + File.separatorChar + picName;
        } else if (picType == PicType.BASE64) {
            //将图片存储在本地
            PicUtils.base64Topic(path + File.separatorChar + picName, content);

            //进行图片是否裁剪  960*960
            PicUtils.resizeImage(path + File.separatorChar + picName, path + File.separatorChar + picName, 960, 960);

            //将图片转base64
            return path + File.separatorChar + picName;
        }
        return null;
    }



    /**
     * 图片 url 输出
     *
     * @param picType  输入图片类型
     * @param content  输入图片内容
     * @param iden     身份
     * @param personId 唯一码
     * @return 返回base64图片
     */
    public static String printUrlPic(PicType picType, String content, String iden, String personId) {
        String picName = iden + "_" + personId + ".jpg";

        String path = Path.save_pic_path + TimeUtils.getNowYear() + File.separatorChar + TimeUtils.getNowMonth() + File.separatorChar + TimeUtils.getNowDay();
        FileUtils.isPathExist(path);

        if (picType == PicType.URL) {
            //先下载图片
            FileUtils.downPic(content, Path.save_pic_path + TimeUtils.getNowYear() + File.separatorChar + TimeUtils.getNowMonth() + File.separatorChar + TimeUtils.getNowDay(), picName);
            //进行图片是否裁剪  960*960
            PicUtils.resizeImage(path + File.separatorChar + picName, path + File.separatorChar + picName, 960, 960);
        } else if (picType == PicType.BASE64) {
            //将图片存储在本地
            PicUtils.base64Topic(path + File.separatorChar + picName, content);

            //进行图片是否裁剪  960*960
            PicUtils.resizeImage(path + File.separatorChar + picName, path + File.separatorChar + picName, 960, 960);

        }
        OssClienUtils.uploadImgAli(picName, new File(path + File.separatorChar + picName));
        return OssClienUtils.getImgAliUrl(picName);

    }


    /**
     * 图片对象 输出
     *
     * @param picType  输入图片类型
     * @param content  输入图片内容
     * @param iden     身份
     * @param personId 唯一码
     * @return 返回base64图片
     */
    public static PictureBean printUrlPic2(PicType picType, String content, String iden, String personId) {
        String picName = iden + "_" + personId + ".jpg";

        PictureBean pictureBean = new PictureBean();
//        pictureBean.setServer(Config.downPicServer);
//        pictureBean.setPort(Config.downPicPort);

        String path = Path.save_pic_path + TimeUtils.getNowYear() + File.separatorChar + TimeUtils.getNowMonth() + File.separatorChar + TimeUtils.getNowDay();
        FileUtils.isPathExist(path);

        if (picType == PicType.URL) {
            //先下载图片
            FileUtils.downPic(content, Path.save_pic_path + TimeUtils.getNowYear() + File.separatorChar + TimeUtils.getNowMonth() + File.separatorChar + TimeUtils.getNowDay(), picName);
            //进行图片是否裁剪  960*960
            PicUtils.resizeImage(path + File.separatorChar + picName, path + File.separatorChar + picName, 960, 960);
        } else if (picType == PicType.BASE64) {
            //将图片存储在本地
            PicUtils.base64Topic(path + File.separatorChar + picName, content);

            //进行图片是否裁剪  960*960
            PicUtils.resizeImage(path + File.separatorChar + picName, path + File.separatorChar + picName, 960, 960);

        }
        OssClienUtils.uploadImgAli(picName, new File(path + File.separatorChar + picName));
        String url = OssClienUtils.getImgAliUrl(picName);
//        if (!TextUtils.isEmpty(url))
//            pictureBean.setPicture(url.replace("http://" + pictureBean.getServer(), ""));
        if (!TextUtils.isEmpty(content))
            pictureBean.setPicture(content.replace("http://" + pictureBean.getServer() + ":" + pictureBean.getPort(), ""));
        return pictureBean;

    }

}
